package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RequestMapping("/clues")
@RestController
public class ClueController {
    @Autowired
    private ClueService clueService;

    //2. 添加线索
    @PostMapping
    public Result addClue(@RequestBody Clue clue){
        log.info("添加线索:{}",clue);
        clue.setStatus(1);//状态待分配
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save(clue);
        return Result.success();
    }

    //1. 线索列表查询
    @GetMapping
    public Result getList(ClueDto clueDto){
        log.info("线索列表请求信息:{}",clueDto);
        PageResult<Clue> pageResult =clueService.listClues(clueDto);
        return Result.success(pageResult);
    }

    //3. 分配线索
    @PutMapping("/assign/{clueId}/{userId}")
    public Result UpdateAssign(@PathVariable Integer clueId,@PathVariable Integer userId){
        log.info("分配线索:{}",clueId);
        Clue clue=new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2);//待跟进
        clue.setUpdateTime(LocalDateTime.now());


        clueService.updateById(clue);
        return Result.success();
    }

    //6. 根据ID查询
    @GetMapping("/{id}")
    public Result getClueById(@PathVariable Integer id){
        log.info("查询线索:{}",id);
        Clue clue = clueService.getClueById(id);
        return Result.success(clue);
    }

    //7. 跟进线索
    @PutMapping
    public Result trackClue(@RequestBody Clue clue) {
        log.info("跟进线索:{}",clue);
        clueService.trackClue(clue);
        return Result.success();
    }

    //4. 伪线索处理
    @PutMapping("/false/{id}")
    public Result falseClue(@PathVariable Integer id, @RequestBody MarkFalseClueDto markFalseClueDto){
        log.info("伪线索处理:{}",id);
        clueService.falseClue(id,markFalseClueDto);
        return Result.success();
    }

    //5. 转商机处理
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        log.info("转商机处理:{}",id);
        clueService.toBusiness(id);
        return Result.success();
    }

    //8. 线索池列表查询
    @GetMapping("/pool")
    public Result getCluePools(ClueDto clueDto){
        log.info("线索池列表查询:{}",clueDto);
        PageResult<Clue> pageList = clueService.getCluePools(clueDto);
        return Result.success(pageList);
    }
}
